import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/eventbus/event_bus.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/pages/category/category_tab_page/entity/category_data.dart';
import 'package:flutter_shop/pages/category/category_tab_page/provider/sub_category_goods_page_provider.dart';
import 'package:flutter_shop/pages/category/category_tab_page/widget/sub_category_goods.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:provider/provider.dart';

///子分类导航
class SubCategoryNav extends StatefulWidget {
  final List<CategoryDataBxMallSubDto> tabs;

  SubCategoryNav({Key key, this.tabs}) : super(key: key);

  @override
  _SubCategoryNavState createState() => _SubCategoryNavState();
}

class _SubCategoryNavState extends BaseState<SubCategoryNav, BasePresenter> {
  ScrollController _scrollController = ScrollController();

  @override
  void didUpdateWidget(SubCategoryNav oldWidget) {
    super.didUpdateWidget(oldWidget);
    _scrollController.jumpTo(0);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().setHeight(80),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Borders.bottom,
      ),
      child: ListView.builder(
        controller: _scrollController,
        itemCount: widget.tabs.length,
        itemBuilder: _buildItem,
        scrollDirection: Axis.horizontal,
      ),
    );
  }

  Widget _buildItem(context, index) {
    CategoryDataBxMallSubDto item = widget.tabs[index];

    //设置文本颜色
    Color textColor = CommonColors.common_text;
    String categorySubId = Provider.of<SubCategoryGoodsPageProvider>(context).categorySubId;

    if (categorySubId == item.mallSubId) {
      textColor = CommonColors.primary;
    } else {
      textColor = CommonColors.common_text;
    }

    return InkWell(
      onTap: () {
        //设置子分类id
        Provider.of<SubCategoryGoodsPageProvider>(context, listen: false).setCategorySubId(item.mallSubId);
        SubCategoryGoodsPageProvider subCategoryGoodsPageProvider = Provider.of<SubCategoryGoodsPageProvider>(context, listen: false);
        //刷新商品列表
        EventBus().post(EventMessage(
          SubCategoryGoods.REFRESH,
          str1: subCategoryGoodsPageProvider.categoryId,
          str2: subCategoryGoodsPageProvider.categorySubId,
        ));
      },
      child: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.only(left: 15, right: 15),
        child: Text(
          item.mallSubName,
          style: TextStyle(color: textColor),
        ),
      ),
    );
  }
}
